<template>
    <div>
        <van-nav-bar title="生成订单" left-arrow @click-left="$router.back()">
            <template #right>
                <van-icon name="ellipsis" size="18" />
            </template>
        </van-nav-bar>
        <!-- 回显地址 -->
        <router-link to="/addressList">
            <div class="address-wrap">
                <div class="name">
                    <span>{{ this.name }}</span>
                    <span>{{ this.tel }}</span>
                </div>
                <van-icon name="arrow" />
                <div class="address">
                    <span>{{ this.address }}</span>
                </div>
            </div>
        </router-link>

        <!-- 商品卡片 -->
        <van-card v-for="item in orderList" :key="item.cartItemId" :num="item.goodsCount" :price="item.sellingPrice"
            :title="item.goodsName" :thumb="item.goodsCoverImg" />

        <!-- 生成订单 -->
        <van-submit-bar :price="totalMoney" label="商品金额" button-text="提交订单" @submit="onSubmit" />

        <!-- 支付面板 -->
        <van-action-sheet v-model="show" :actions="actions" cancel-text="取消" close-on-click-action @cancel="onCancel"
            @select="onSelect" />
    </div>
</template>

<script>
import { getAddressDetail, getOrderCard, saveOrder, paySuccess } from "../../api/index.js"
import { Toast } from 'vant'
export default {
    name: "CreateOrder",
    props: [],
    created() {
        this.addressId = this.$route.query.addressId;
        this.getAddress();
        this.getOrder();
    },
    data() {
        return {
            name: "", // 收货人姓名
            tel: "", // 手机号
            address: "", // 地址
            orderList: [], // 购物车中选中的商品
            cartItemIds: [], // 购物车中选中商品的ID
            orderNumber: "", // 订单号
            show: false, // 支付面板是否显示
            actions: [{ name: "微信支付" }, { name: "支付宝支付" }],
        };
    },
    computed: {
        totalMoney() {
            return this.orderList.reduce((prev, next) => {
                return (
                    prev + next.goodsCount * next.sellingPrice * 100
                );
            }, 0);
        },
    },
    methods: {
        // 获取某个地址数据
        async getAddress() {
            // 根据ID请求地址详情
            let data = await getAddressDetail(this.addressId)
            this.name = data.data.userName;
            this.tel = data.data.userPhone;
            this.address =
                data.data.provinceName +
                data.data.cityName +
                data.data.regionName +
                data.data.detailAddress;
        },
        // 获取购物车中选中的商品
        getOrder() {
            getOrderCard(localStorage.getItem("cartItemIds")).then(data => {
                this.orderList = data.data;
            })
        },
        // 点击生成订单
        onSubmit() {
            // 准备地址ID和商品ID
            // 从localStorage中就可以得到的ID
            this.cartItemIds = localStorage.getItem("cartItemIds").split(",");
            // console.log(this.cartItemIds);
            // this.addressId 是地址ID 
            // console.log(this.addressId);

            // 弹出支付面板
            this.show = true;

            saveOrder({ addressId: this.addressId, cartItemIds: this.cartItemIds }).then(res => {
                // console.log("res:", res);
                this.orderNumber = res.data;
            })

        },
        // 点击取消支付
        onCancel() {
        },
        // 选择不同的支付方式
        onSelect(item) {
            if (item.name == "微信支付") {
                // 理论上，需要调用支付接口，才能支付成功，模拟一下
                paySuccess(this.orderNumber, 1).then(res => {
                    // console.log("res:", res);
                    // 提示支付成功
                    Toast.loading({
                        message: '支付成功',
                    });
                    setTimeout(() => {
                        // 提示支付成功
                        this.$router.replace("/order")
                    }, 500)
                })
            } else if (item.name == "支付宝支付") {
                // 理论上，需要调用支付接口，才能支付成功，模拟一下
                paySuccess(this.orderNumber, 2).then(res => {
                    // console.log("res:", res);
                    // 提示支付成功
                    Toast.loading({
                        message: '支付成功',
                    });
                    setTimeout(() => {
                        // 提示支付成功
                        this.$router.replace("/order")
                    }, 500)
                })
            }
            localStorage.removeItem("cartItemIds")
        }
    },
};
</script>

<style lang="less" scoped>
.van-nav-bar {
    position: fixed;
    width: 100%;
    height: 1.17333rem;
    top: 0;
    left: 0;
    z-index: 100;
    line-height: 1.17333rem;
    padding: 0 0.26667rem;
    box-sizing: border-box;
    background: #fff;
    border-bottom: 0.02667rem solid #dcdcdc;
}

.address-wrap {
    margin-top: 1.17333rem;
    margin-bottom: 0.53333rem;
    background: #fff;
    position: relative;
    font-size: 0.37333rem;
    padding: 0.63rem 0.4rem 0.66667rem;
    color: #222333;

    .name {
        display: flex;
        justify-content: left;
        margin: 0.26667rem 0 0.27667rem;
        line-height: 0.37333rem;

        span:nth-child(1) {
            margin-right: 0.04rem;
        }
    }

    .address {
        display: flex;
        justify-content: left;
        line-height: 0.37333rem;
    }

    .van-icon {
        position: absolute;
        right: 0.26667rem;
        top: 50%;
        -webkit-transform: translateY(-50%);
        transform: translateY(-50%);
        font-size: 0.53333rem;
    }
}

.address-wrap::before {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    height: 0.05333rem;
    background: -webkit-repeating-linear-gradient(135deg,
            #ff6c6c,
            #ff6c6c 20%,
            transparent 0,
            transparent 25%,
            #1989fa 0,
            #1989fa 45%,
            transparent 0,
            transparent 50%);
    background: repeating-linear-gradient(-45deg,
            #ff6c6c,
            #ff6c6c 20%,
            transparent 0,
            transparent 25%,
            #1989fa 0,
            #1989fa 45%,
            transparent 0,
            transparent 50%);
    background-size: 2.13333rem;
    content: "";
}

.van-card__content {
    display: flex;
    justify-content: center;

    .van-card__title {
        text-align: left;
    }

    .van-multi-ellipsis--l2 {
        margin-bottom: 22px;
    }

    .van-card__bottom {
        display: flex;
        justify-content: space-between;

        span {
            color: red;
        }
    }
}

.van-submit-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background: #fff;
    padding: 0.26667rem 0;
    padding-bottom: 1.33333rem;
    border-top: 0.02667rem solid #e9e9e9;

    .van-submit-bar__text {
        display: flex;
        -webkit-box-pack: justify;
        -webkit-justify-content: space-between;
        justify-content: space-between;
        padding: 0 5%;
        margin: 0.26667rem 0;
        font-size: 0.37333rem;
    }

    .van-button {
        position: fixed;
        bottom: 0.18667rem;
        right: 0;
        left: 0;
        width: 90%;
        margin: 0 auto;
        border-radius: 5px;
    }
}
</style>